package com.atguigu.daijia.rules.client.fallback;

import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.model.form.rules.ProfitsharingRuleRequestForm;
import com.atguigu.daijia.model.vo.rules.ProfitsharingRuleResponseVo;
import com.atguigu.daijia.rules.client.ProfitsharingRuleFeignClient;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * @create: 2024/9/29
 * @author: gyhe10
 * @description: ProfitsharingRuleFeignClient 接口的降级实现工厂类
 **/
@Component
public class ProfitsharingRuleFeignClientFallbackFactory implements FallbackFactory<ProfitsharingRuleFeignClient> {
    @Override
    public ProfitsharingRuleFeignClient create(Throwable cause) {
        return new ProfitsharingRuleFeignClient() {
            @Override
            public Result<ProfitsharingRuleResponseVo> calculateOrderProfitsharingFee(ProfitsharingRuleRequestForm profitsharingRuleRequestForm) {
                if (cause instanceof GuiguException) {
                    return Result.build(null, ((GuiguException) cause).getCode(), cause.getMessage());
                } else {
                    return Result.build(null, ResultCodeEnum.FEIGN_FAIL);
                }
            }
        };
    }
}
